package david.java.tools;


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.MessageFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: David
 * @Date: Create in 4:41 下午 2021/11/8
 */
public class PartitionGenerator {

    private final static DateTimeFormatter FORMAT = DateTimeFormatter.ofPattern("yyyyMMdd");

    // private static String BEGIN_DATE = "20221202";
    private static String BEGIN_DATE = "20230501";
    private static Integer DAY = 30;


    public static void main(String[] args) throws IOException {
        String tarPath = "target-tmp/partition";
        mkdirIfNotExists(tarPath);
        if (BEGIN_DATE.equals("")) {
            BEGIN_DATE = getDateKey(null, null, -5);
        }
        List<String> partitionDate = getPartitionDate(BEGIN_DATE, DAY);
        generate(partitionDate, tarPath);
    }

    private static void mkdirIfNotExists(String path) {
        File file = new File(path);
        String parent = file.getParent();
        if (parent.equals("/")) {
            return;
        }
        boolean exists = file.getParentFile().exists();
        if (!exists) {
            file.getParentFile().mkdirs();
        }
    }

    private static List<String> getPartitionDate(String begin, int days) {
        List<String> list = new ArrayList<>();

        list.add(MessageFormat.format("\tPARTITION p{0} VALUES  [(\"{1}\"),(\"{2}\")),", begin, "19700101", begin));

        LocalDate b = LocalDate.parse(begin, FORMAT);
        LocalDate e = null;

        for (int i = 0; i < days; i++) {

            e = b.plusDays(1);
            list.add(MessageFormat.format(i == days - 1 ? "\tPARTITION p{0} VALUES  [(\"{1}\"),(\"{2}\"))"
                    : "\tPARTITION p{0} VALUES  [(\"{1}\"),(\"{2}\")),", e.format(FORMAT), b.format(FORMAT), e.format(FORMAT)));
            b = e;
        }


        return list;
    }

    private static void generate(List<String> list, String tarParth) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter(tarParth));

        list.forEach(t -> {
            try {
                bw.write(t);
                bw.newLine();
            } catch (IOException e) {
                e.printStackTrace();
                try {
                    bw.close();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
        });

        bw.close();
    }

    private static String getDateKey(Integer plusYear, Integer plusMonth, Integer plusDay) {
        LocalDate now = LocalDate.now();
        if (plusYear != null) {
            now = now.plusDays(plusYear);
        }
        if (plusMonth != null) {
            now = now.plusDays(plusMonth);
        }
        if (plusDay != null) {
            now = now.plusDays(plusDay);
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        return formatter.format(now);
    }


}
